<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataPicType extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $db = Db::name('data_pic_type'); 
        if(!empty($param['parent_typeid'])) {
            $db = $db->where('parent_typeid',$param['parent_typeid']);
            if(!empty($param['typeid'])){
                $db = $db->where('typeid',$param['typeid']);
            }
        }
        if(!empty($param['kw'])){
            $db = $db->where('project','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $param['parent_typeid']
        ])->select()->toArray();
        return view('data_pic/index',['list'=>$list,'count'=>$count,'param'=>$param,'parent_typelist'=>$parent_typelist,'typelist'=>$typelist]);
    }

    public function add()
    {
        if( $this->request->isAjax() ){
            $param = $this->request->param();
            $file = $this->request->file('pic');
            $folder = config('filesystem.disks.folder') . '/pic'; //存文件目录
            $savename = \think\facade\Filesystem::disk('public')->putFile($folder,$file);
            if($savename){
                if(!empty($param['parent_typeid'])) $param['top_typeid'] = get_typepid($param['parent_typeid']);
                $param['url'] = '/' . str_replace("\\","/",$savename);
                $param['file_name'] = $file->getOriginalName();
                $param['file_size'] = $file->getSize();
                $param['extension'] = strtolower(pathinfo($file->getOriginalName(), PATHINFO_EXTENSION));
                $r = Db::name('data_pic_type')->insert($param);
                if($r){
                    return json(['status' => 1, 'msg' => '上传成功', 'data' => $param['file_name']]);
                }
                return json(['status' => 0, 'msg' => '入库失败']);
            }
            return json(['status' => 0, 'msg' => '移动文件失败']);
        }
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        return view('data_pic/add', ['parent_typelist'=>$parent_typelist]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = Db::name('data_pic_type')->update($param);
            if( $result ) {
                xn_add_admin_log('修改图片');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        $data = Db::name('data_pic_type')->where('id',$id)->find();
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $data['parent_typeid']
        ])->select()->toArray();
        return view('data_pic/edit',['data'=>$data,'parent_typelist'=>$parent_typelist,'typelist'=>$typelist]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        $file_data = Db::name('data_pic_type')->find($id);
        $file_path = xn_root()."/".ltrim($file_data->url,'/');
        if( file_exists($file_path) ) {
            unlink($file_path);
        }
        Db::name('data_pic_type')->delete($id);
        xn_add_admin_log('删除图片');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        $file_data = Db::name('data_pic_type')->where('id','in',$ids)->select();
        foreach($file_data as $v){
            $file_path = xn_root()."/".ltrim($v->url,'/');
            if( file_exists($file_path) ) {
                unlink($file_path);
            }
        }
        Db::name('data_pic_type')->delete($ids);
        xn_add_admin_log('批量删除图片');
        $this->success("删除成功");
    }

    public function move(){
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        $data = Db::name('data_pic_type')->find($id);
        unset($data['id']);
        $r = Db::name('data_pic_project')->insert($data);
        if($r){
            Db::name('data_pic_type')->delete($id);
        }
        xn_add_admin_log('移动图片');
        $this->success('移动成功');
    }

    public function moveAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        $datas = Db::name('data_pic_type')->where('id','in',$ids)->select();
        foreach($datas as $data){
            $id = $data['id'];
            unset($data['id']);
            $r = Db::name('data_pic_project')->insert($data);
            if($r){
                Db::name('data_pic_type')->delete($id);
            }
        }
        xn_add_admin_log('批量移动图片');
        $this->success('移动成功');
    }

}
